package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.DishFlavor;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.concurrent.ConcurrentUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;

@Service
@Slf4j
@Transactional
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public Page<Dish> selectByPage(String name, Integer pageNum, Integer pageSize) {

        //1.构建查询条件 name like
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNoneEmpty(name), Dish::getName, name);//name不为空时查询条件才成立

        //2.构建分页条件 page
        Page<Dish> page = new Page<>(pageNum, pageSize);

        //3.执行根据name分页查询菜品信息列表
        page = dishMapper.selectPage(page, wrapper);

        //4.遍历结果中的菜品集合，拿到每一样菜品
        List<Dish> dishList = page.getRecords();
        if (CollectionUtil.isNotEmpty(dishList)) {
            for (Dish dish : dishList) {
                //1.getCategoryId获取到菜品的种类,并添加到dish中
                Category category = categoryMapper.selectById(dish.getCategoryId());
                dish.setCategoryName(category.getName());
                //2.获取菜品口味
                LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(DishFlavor::getDishId, dish.getId());
                List<DishFlavor> dishFlavorList = dishFlavorMapper.selectList(wrapper1);
                dish.setFlavors(dishFlavorList);//封装当前菜品的口味列表
            }
        }
        return page;
    }

    @Override
    public void save(Dish dish) {
        //1.将dish信息保存
        dishMapper.insert(dish);
        //2.将口味的DishId修改为菜品的id
        List<DishFlavor> flavorList = dish.getFlavors();
        if (CollectionUtil.isNotEmpty(flavorList)) {
            for (DishFlavor dishFlavor : flavorList) {
                dishFlavor.setDishId(dish.getId());
                dishFlavorMapper.insert(dishFlavor);
            }
        }
    }

    //根据id查询
    @Override
    public Dish findById(Long id) {
        Dish dish = dishMapper.selectById(id);
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId, id);
        dish.setFlavors(dishFlavorMapper.selectList(wrapper));
        return dish;
    }

    @Override
    public void update(Dish dish) {
        dishMapper.updateById(dish);

        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId, dish.getId());
        dishFlavorMapper.delete(wrapper);

        List<DishFlavor> flavorList = dish.getFlavors();
        if (CollectionUtil.isNotEmpty(flavorList)) {
            for (DishFlavor dishFlavor : flavorList) {
                dishFlavor.setDishId(dish.getId());
                dishFlavorMapper.insert(dishFlavor);
            }
        }
    }

    //停用/启用方法
    @Override
    public void status(Integer status, List<Long> ids) {
        for (Long id : ids) {
            Dish dish = dishMapper.selectById(id);
            dish.setStatus(status);
            dishMapper.updateById(dish);
        }
    }

    //根据id和status=1，获取获取菜品数据
    @Override
    public List<Dish> findList(String name, Long categoryId, Integer status) {
        //1. 设置查询条件
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name), Dish::getName, name);
        wrapper.eq(categoryId != null, Dish::getCategoryId, categoryId);
        wrapper.eq(Dish::getStatus, status);
        List<Dish> dishList = dishMapper.selectList(wrapper);
        //2. 遍历dishList
        for (Dish dish : dishList) {
            LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(DishFlavor::getDishId, dish.getId());
            List<DishFlavor> dishFlavorList = dishFlavorMapper.selectList(wrapper1);
            dish.setFlavors(dishFlavorList);
            dishMapper.updateById(dish);
        }
        return dishList;
    }
    //根据id和status=1，获取获取菜品数据
    @Override
    public List<Dish> findList(Long categoryId, Integer status) {
        //1. 设置查询条件
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(categoryId != null, Dish::getCategoryId, categoryId);
        wrapper.eq(Dish::getStatus, status);
        List<Dish> dishList = dishMapper.selectList(wrapper);
        //2. 遍历dishLists
        for (Dish dish : dishList) {
            LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(DishFlavor::getDishId, dish.getId());
            List<DishFlavor> dishFlavorList = dishFlavorMapper.selectList(wrapper1);
            dish.setFlavors(dishFlavorList);
            dishMapper.updateById(dish);
        }
        return dishList;
    }

    //删除方法
    @Override
    public void deleteByIds(Long[] ids) {
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Dish::getId, ids);
        wrapper.eq(Dish::getStatus, 1);
        int count = dishMapper.selectCount(wrapper);
        if (count > 0) {
            throw new CustomException("菜品还在销售,无法删除");
        }
        for (Long id : ids) {
            LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(DishFlavor::getDishId, id);
            dishFlavorMapper.delete(wrapper1);

            dishMapper.deleteById(id);
        }
    }
}
